import { fallbackRequestGet, fallbackRequestPost } from '@/utils/Request'
import { addURLParam } from '@utils/Utils'

/**
 * 请求前缀，后面后台添加网关后需要前缀选择微服务
 */
const URL_PREFIX = 'middle-goods/'

/**
 * 添加订单
 * @param {GoodOrderInsertDTO} dto 添加订单必须数据
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const insertGoodOrderDTO = (dto, successCallback = null, fullCallback = null) => {
  return fallbackRequestPost(URL_PREFIX + 'order_detail/insertGoodOrderDTO', dto, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 订单支付
 * @param {GoodOrderInsertDTO} id 订单id
 */
export const payOrderById = (id) => {
  const url = window.location.href
  const returnUrl = url.substring(0, url.indexOf('#')) + '#/account/order'
  let toUrl = 'http://lixunda.top:2100/middle-goods/order_detail/payOrderById'
  toUrl = addURLParam(toUrl, 'id', id)
  toUrl = addURLParam(toUrl, 'returnUrl', returnUrl)
  console.log(toUrl)
  window.open(toUrl)
}

/**
 * 分页条件查询订单
 * @param {PageDTO<OrderDetailQueryDTO>} pageDTO 订单分页条件查询添加DTO
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const queryOrderDetailPageWrapper = (pageDTO, successCallback = null, fullCallback = null) => {
  return fallbackRequestPost(URL_PREFIX + 'order_detail/queryOrderDetailPageWrapper', pageDTO, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 根据id取消订单
 * @param {Array<Number|String>} ids 取消订单的订单编号
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const cancelOrderByIds = (ids, successCallback = null, fullCallback = null) => {
  return fallbackRequestPost(URL_PREFIX + 'order_detail/cancelOrderByIds', ids, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 用户调用，申请退款，申请后由店家验证退款
 * @param {Number|String} orderId 订单编号
 * @param {String} message 申请理由
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const applyRefundByOrderId = (orderId, message = '', successCallback = null, fullCallback = null) => {
  return fallbackRequestGet(URL_PREFIX + 'order_detail/applyRefundByOrderId', {
    orderId,
    message
  }, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 用户调用，根据申请id取消退款申请
 * @param {Number|String} id 退款申请id
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const applyRefundCancelById = (id, successCallback = null, fullCallback = null) => {
  return fallbackRequestGet(URL_PREFIX + 'order_detail/applyRefundCancelById', {
    id
  }, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 用户调用，根据订单id取消退款申请
 * @param {Number|String} orderId 订单id
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const applyRefundCancelByOrderId = (orderId, successCallback = null, fullCallback = null) => {
  return fallbackRequestGet(URL_PREFIX + 'order_detail/applyRefundCancelByOrderId', {
    orderId
  }, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 用户调用，根据订单编号移除订单
 * @param {Number|String} id 订单id
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const removeOrderUserById = (id, successCallback = null, fullCallback = null) => {
  return fallbackRequestGet(URL_PREFIX + 'order_detail/removeOrderUserById', {
    id
  }, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}
